<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Customer_Report extends CI_Controller {
	public function index(){
		#$this->session->userdata('user_id').'|'.$this->session->userdata('user_name');
		$this->layout->setLayout('default');
		$this->layout->setTitle('Customer Website | Report');
		
		$month = isset($_GET['month']) ? date('Y').'-'.str_pad(quotes_to_entities($_GET['month']), 2, "0", STR_PAD_LEFT) : date('Y-m');
		
		$arrDate = explode("-", $month);
		$data['month'] = $arrDate[1].'-'.$arrDate[0];
		//Tinh so ngay trong thang
		$day = cal_days_in_month(CAL_GREGORIAN, $arrDate[1],$arrDate[0]);
		$total = 0;
		$arrData = array();
		$arrData1 = array();
		$sqlsms = "Select sms_code From tdx_website Where 1=1 And customer_id = " . $_GET['cus'];
		$webQuery = $this->db->query($sqlsms);
		if ($webQuery->num_rows() > 0){
			$filcode = "";
			
			$j = 1;
			for ($i=0 ; $i<=($webQuery->num_rows - 1); $i++ ):
				$row = $webQuery->row($i); 
				$filcode = $row->sms_code;
				
				// Use Procedure in Phpmyadmin ======================================
				$con = mysql_connect($this->db->hostname, $this->db->username, $this->db->password, false,65536);
				mysql_select_db($this->db->database, $con);
				
				$sql = "call spSubTotalMonthSite('".$month."-01 00:00:00','".$month."-".$day." 23:59:59','".$filcode."');";
				$queryUser1 = mysql_query($sql);
				
				if ($queryUser1 === FALSE) {
					die(mysql_error());
				}
				
				while($row = mysql_fetch_array($queryUser1))
				{				
					$data['item'][$j][] = $row;
					$arrData[$row['earn_date']] += intval($row['earm_day']);
					$arrData1[$row['message_request']] += intval($row['earm_day']);
					$total += intval($row['earm_day']);
				}
				
				mysql_close($con);
				// ==================================================================							
				$j += 1;
			endfor;
			$data['loop'] = $j - 1;

		}else{
			$filcode = "" . $this->session->userdata('sms_code') . "";	
			// Use Procedure in Phpmyadmin ======================================
			$con = mysql_connect($this->db->hostname, $this->db->username, $this->db->password, false,65536);
			mysql_select_db($this->db->database, $con);
			
			$sql = "call spSubTotalMonthSite('".$month."-01 00:00:00','".$month."-".$day." 23:59:59','".$filcode."');";
			$queryUser1 = mysql_query($sql);
			
			if ($queryUser1 === FALSE) {
				die(mysql_error());
			}
			
			while($row = mysql_fetch_array($queryUser1))
			{				
				$data['items1'][] = $row;
				$arrData[$row['earn_date']] += intval($row['earm_day']);
				$arrData1[$row['message_request']] += intval($row['earm_day']);
				$total += intval($row['earm_day']);
			}
			
			mysql_close($con);
			// ==================================================================
			$data['loop'] = 1;
		}
			
		$wid = intval($this->uri->segment(1));	
		
		
		if(isset($_GET['date'])){
			$today = $_GET['date'];
			$strDate = 'Ngày '.$_GET['date'];
			$data['str_date'] = $strDate;
			// Use Procedure in Phpmyadmin ======================================
			$con = mysql_connect($this->db->hostname, $this->db->username, $this->db->password, false,65536);
			mysql_select_db($this->db->database, $con);
			$sql1 = "call spSubTotalMonth('".$today." 00:00:00','".$today." 23:59:59','".$filter."');";
			$queryUser1 = mysql_query($sql1);
			if ($queryUser1 === FALSE) {
				die(mysql_error());
				$data['earn_today'] = 0;
			}
			
			while($row = mysql_fetch_array($queryUser1))
			{				
				$data['earn_today'] += intval($row['earm_day']);
			}
			
			mysql_close($con);
			// ==================================================================			
			
		}
		else{
			$strDate = 'Hôm nay ('.date('d-m-Y').')';
			$data['earn_today'] = intval($arrData[date('d-m-Y')]);
		}
		$data['earn_yesterday'] = intval($arrData[date('d-m-Y', strtotime('-1 day'))]);
		$data['earn_this_month'] = $total;
		$data['str_date'] = $strDate;
		$data['month_data'] = $arrData;
		$data['month_data_sms'] = $arrData1;
		$data['currentUser'] = $this->session->userdata('user_name');
		$this->layout->view(THEME_DIR.'/customer_report_index', $data);
	}
}